<template>
  <!-- // 分配角色 -->
  <div>
    <!-- 这里将来会放置多选列表 -->
    {{ checkList }}
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleIds" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings.js'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees.js'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    },
    showRole: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      checkList: []
    }
  },
  async created() {
    // 显示所有角色列表
    const res = await getRoles(1, 10)
    console.log(res)
    this.roleIds = res.data.rows
    this.loadEmployee()
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog')
    },
    // 点击确认
    async hSubmit() {
      // this.$emit('closeDialog')
      // this.roleIds = []
      const res = await assignRoles({
        id: this.employeeId,
        roleIds: this.checkList
      })
      console.log(res)
      this.$emit('update:showRole', false)
    },
    async loadEmployee() {
      const res = await getUserDetailById(this.employeeId)
      console.log(res)
      this.checkList = res.data.roleIds
    }
  }
}
</script>
